<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\grid\GridView;
use backend\models\Contract;
use backend\helpers\ActionButton;
use backend\helpers\FileViewHelper;
use backend\helpers\SimpleFlagHelper;

/* @var $this yii\web\View */
/* @var $model backend\models\Contract */

$this->title = "Договор №" . Html::encode($model->own_number);
$this->params['breadcrumbs'][] = ['label' => 'Договоры', 'url' => ['index']];
$this->params['breadcrumbs'][] = "№" . Html::encode($model->own_number);
\yii\web\YiiAsset::register($this);
?>

<div class="container contract-view">

    <h1 class="main-title">Договор №<?= Html::encode($model->own_number) ?></h1>
    
    <div class="action-menu action-menu__container">
        <div>
            <?= ActionButton::create('Добавить новый договор', 'createContract') ?>
            <?= ActionButton::custom('Добавить документ в договор', 'createContract', 'add-file', ['id' => $model->id])?>
        </div>
        <div>
            <?= ActionButton::update($model->id, 'updateContract') ?>
            <?= ActionButton::delete($model->id, 'Вы точно хотите удалить договор?', 'deleteContract') ?>
        </div>
    </div>


    <?= DetailView::widget([
        'model' => $model,
        'options' => ['class' => 'table table-striped detail-view'],
        'attributes' => [
            'id',
            'registry_number',
            'own_number',
            'contractor_number',
            [
                'attribute' => 'id_status',
                'value' => function($model) { 
                    return '<span class="badge ' . $model->status->css_style . '">' . $model->status->title . '</span>'; 
                },
                'format' => 'raw',
            ],
            [
                'label' => 'Контрагент',
                'value' => function($model) {
                    return HTML::a($model->contractor->name_full_opf, ['contractor/view', 'id' => $model->contractor->id], ['target' => '_blank']);
                },
                'format' => 'raw',
            ],
            'subject',
            'total_price:currency',
            [
                'label' => 'Источник финансирования',
                'attribute' => 'financeSource.name',
            ],
            [
                'label' => 'Способ закупки',
                'attribute' => 'purchaseMethod.title',
            ],
            [
                'label' => 'Цель закупки',
                'attribute' => 'purpose.title',
            ],
            [
                'label' => 'Донор',
                'attribute' => 'donor.name',
            ],
            [
                'label' => 'Проект',
                'attribute' => 'project.title',
            ],
            [
                'label' => 'Ответственный сотрудник',
                'attribute' => 'liableUser.profile.name',
            ],
            [
                'label' => 'Филиал',
                'attribute' => 'organizationBranch.title',
            ],
            'date_sign:date',
            'date_realize:date',
            [
                'attribute'=>'is_yearly',
                'value' => function ($model) {
                    return SimpleFlagHelper::statusLabel($model->is_yearly);
                },
                'format' => 'raw'
            ],
            'comment:ntext',
        ],
    ]) ?>

    <!-- <h2 class="table-header">Согласование</h2>
    <table id="w1" class="table table-striped detail-view table-headered">
        <tbody>
            <tr><th>Лозовой Г.А.</th><td>Не ознакомлен</td></tr>
            <tr><th>Лоскутов Э.В.</th><td>Согласовано</td></tr>
        </tbody>
    </table> -->

    <h2 class="table-header">Документооборот - первый этап</h2>
    
    <?= DetailView::widget([
        'model' => $model,
        'options' => ['class' => 'table table-striped detail-view table-headered'],
        'attributes' => [
            [
                'attribute'=>'doc_kp_answer_one',
                'value' => FileViewHelper::simpleWithName(Contract::BUCKET_NAME, $model->doc_kp_answer_one),
                'format' => 'raw'
            ],
            [
                'attribute'=>'doc_kp_answer_two',
                'value' => FileViewHelper::simpleWithName(Contract::BUCKET_NAME, $model->doc_kp_answer_two),
                'format' => 'raw'
            ],
            [
                'attribute'=>'doc_kp_screenshot',
                'value' => FileViewHelper::simpleWithName(Contract::BUCKET_NAME, $model->doc_kp_screenshot),
                'format' => 'raw'
            ],
        ],
    ]) ?>


    <h2 class="table-header">Документооборот - второй этап</h2>

    <?= DetailView::widget([
        'model' => $model,
        'options' => ['class' => 'table table-striped detail-view table-headered'],
        'attributes' => [
            [
                'attribute'=>'doc_main_contract',
                'value' => FileViewHelper::simpleWithName(Contract::BUCKET_NAME, $model->doc_main_contract),
                'format' => 'raw'
            ],
        ],
    ]) ?>

<h2 class="table-header">Документооборот - третий этап</h2>

<?= DetailView::widget([
    'model' => $model,
    'options' => ['class' => 'table table-striped detail-view table-headered'],
    'attributes' => [
        [
            'attribute'=>'doc_invoice',
            'value' => FileViewHelper::simpleWithName(Contract::BUCKET_NAME, $model->doc_invoice),
            'format' => 'raw'
        ],
        [
            'attribute'=>'doc_close',
            'value' => FileViewHelper::simpleWithName(Contract::BUCKET_NAME, $model->doc_close),
            'format' => 'raw'
        ],
        [
            'attribute'=>'doc_payment_order',
            'value' => FileViewHelper::simpleWithName(Contract::BUCKET_NAME, $model->doc_payment_order),
            'format' => 'raw'
        ],
    ],
]) ?>

<h2 class="table-header">Дополнительные документы</h2>

<?= GridView::widget([
    'dataProvider' => $filesDataProvider,
    'showHeader'=> false,
    'tableOptions' => [
        'class' => 'table table-striped table-headered'
    ],
    'layout' => '{items}', 
    'columns' => [
        [
            //'label' => 'Дополнительные документы',
            'value' => function ($model) { return FileViewHelper::simpleWithName(\backend\models\ContractFile::BUCKET_NAME, $model->filename); },
            'format' => 'raw',
        ],
        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '{delete}',
            'visibleButtons' =>
            [
                'delete' => Yii::$app->user->can('deleteContract'),
            ],
            'urlCreator' => function ($action, $model, $key, $index) {
                if ($action === 'delete') {
                    return \yii\helpers\Url::to(['contract/delete-file', 'id' => $model->id]);
                }
            },
        ],
    ],
]);?>

</div>
